//
//  CircleTestViewController.m
//  CircleTest
//
//  Created by iphone user on 4/5/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "CircleTestViewController.h"
#import "gestureOverlay.h"
#import <QuartzCore/QuartzCore.h>

@implementation CircleTestViewController
@synthesize  imgView;

-(IBAction) button1:(id)sender{
	
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"1.jpg"];
	prevImg=@"1.jpg";
}
-(IBAction) button2:(id)sender{
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"2.jpg"];
	prevImg=@"2.jpg";
}
-(IBAction) button3:(id)sender{
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"3.jpg"];
	prevImg=@"3.jpg";
}
-(IBAction) button4:(id)sender{
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"4.jpg"];
	prevImg=@"4.jpg";
}
-(IBAction) button5:(id)sender{
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"5.jpg"];
	prevImg=@"5.jpg";
}
-(IBAction) button6:(id)sender{
	AudioServicesPlaySystemSound (soundtockID);
	imgView.image =[UIImage imageNamed:@"6.jpg"];
	prevImg=@"6.jpg";
}

-(void)animate:(UIView *)viewToAnimate HideAni:(BOOL)hide withAnimation:(NSString *) aAnimation withSound:(SystemSoundID)sound;
{
	
	AudioServicesPlaySystemSound (sound);
    // First create a CATransition object to describe the transition
    CATransition *transition = [CATransition animation];
    // Animate over 3/4 of a second
    transition.duration = 0.25;
    // using the ease in/out timing function
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    // Now to set the type of transition. Since we need to choose at random, we'll setup a couple of arrays to help us.
    transition.type = kCATransitionPush;
	transition.subtype = aAnimation;
    
    // Finally, to avoid overlapping transitions we assign ourselves as the delegate for the animation and wait for the
    // -animationDidStop:finished: message. When it comes in, we will flag that we are no longer transitioning.
    transitioning = YES;
    transition.delegate = self;
    
    // Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
    [viewToAnimate.layer addAnimation:transition forKey:nil];
    
    viewToAnimate.hidden = hide;
}  
  


-(void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    transitioning = NO;
}



-(void) onGesture:(int) gesture{
	
	if(showingBigMic && gesture == kLineDown)
	{
		[self animate:bigMic HideAni:YES withAnimation:kCATransitionFromBottom withSound: soundClickID];
		showingBigMic=NO;
	}
	else if(showingLighter)
	{
		switch (gesture) {
			case kLineLeft:
				if(lighterView.hidden==NO)
				{
					[self animate:lighterView HideAni:YES withAnimation:kCATransitionFromRight withSound: soundClickID];
				}
				if(!lighterOpenView.hidden)
				{
					[self animate:lighterOpenView HideAni:YES withAnimation:kCATransitionFromRight withSound: soundClickID];
				}
				if(!lighterBurnView.hidden)
				{
					[self animate:lighterBurnView HideAni:YES withAnimation:kCATransitionFromRight withSound: soundClickID];
				}
				showingLighter =NO;
			break;
			case kLineDown:
			case kLineUp:
				if(lighterOpenView.hidden==NO)
				{
					lighterBurnView.hidden=NO;
					lighterOpenView.hidden=YES;
					AudioServicesPlaySystemSound (soundtockID);
					
				}
				break;

			case kFlipOut:
				if(lighterView.hidden==NO)
				{
					lighterOpenView.hidden=NO;
					lighterView.hidden=YES;
					AudioServicesPlaySystemSound (soundClickID);
				}
				break;
			case kFlipIn:
				if(lighterView.hidden==YES)
				{
					lighterBurnView.hidden=YES;
					lighterOpenView.hidden=YES;
					lighterView.hidden=NO;
					AudioServicesPlaySystemSound (soundClickID);
				}
				break;
			default:
				break;
		}
	}
	else
	{
		switch (gesture) {
			case kCircle:
					[self showMic];
				break;
			case kLineUp:
				[self animate:bigMic HideAni:NO withAnimation:kCATransitionFromTop withSound: soundClickID];
				showingBigMic=YES;
				break;
			case kLineRight:
				
				if(!showingLighter)
				{
					[self animate:lighterView HideAni:NO withAnimation:kCATransitionFromLeft withSound: soundClickID];
					showingLighter =YES;
				}
				break;
			case kCross:
				AudioServicesPlaySystemSound (soundtockID);
				imgView.image =[UIImage imageNamed:prevImg];
				break;
			default:
				break;
		}
	}
}
-(void) showMic{
	AudioServicesPlaySystemSound (soundClickID);
    
	if(alt)
	{
		imgView.image =[UIImage imageNamed:@"mic.jpg"];
	}
	else 
	{
		imgView.image =[UIImage imageNamed:@"mic2.png"];
	}
	alt = !alt;
}



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.userInteractionEnabled = YES;
	self.title = @"Gesture";
	gestureOverlay * tv = [[gestureOverlay alloc] initWithView:self.view];
	[tv setGestureController:self];
	[tv setDrawGesture:NO];
	
	alt =YES;
	prevImg=@"1.jpg";
	NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"BigMic" owner:self options:nil];
	bigMic = [nibViews objectAtIndex:0];
	[bigMic setHidden:YES];
	showingBigMic = NO;
	[self.view addSubview:bigMic];
	
	NSArray* nibViews1 =  [[NSBundle mainBundle] loadNibNamed:@"lighterView" owner:self options:nil];
	lighterView = [nibViews1 objectAtIndex:0];
	[lighterView setHidden:YES];
	showingLighter = NO;
	[self.view addSubview:lighterView];
	
	
	NSArray* nibViews2 =  [[NSBundle mainBundle] loadNibNamed:@"lighterOpen" owner:self options:nil];
	lighterOpenView = [nibViews2 objectAtIndex:0];
	[lighterOpenView setHidden:YES];
	[self.view addSubview:lighterOpenView];
	
	
	NSArray* nibViews3 =  [[NSBundle mainBundle] loadNibNamed:@"lighterBurn" owner:self options:nil];
	lighterBurnView = [nibViews3 objectAtIndex:0];
	[lighterBurnView setHidden:YES];
	[self.view addSubview:lighterBurnView];
	
	[self.view addSubview:tv];
	[tv release];
	transitioning =NO;
	
	NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Tock" ofType:@"aiff"];
	
	AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundtockID);
	
	soundPath = [[NSBundle mainBundle] pathForResource:@"photoShutter" ofType:@"aiff"];
	
	AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundClickID);
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
